 package com.suozq.datastructure.tree;

 /**
 * @author 91585
 * @date 2022/05/05
 */
public class TreeNode<T> {
    
    private T val;
    private TreeNode<T> left;
    private TreeNode<T> right;
    
    public T getVal() {
        return val;
    }
    public void setVal(T val) {
        this.val = val;
    }
    public TreeNode<T> getLeft() {
        return left;
    }
    public void setLeft(TreeNode<T> left) {
        this.left = left;
    }
    public TreeNode<T> getRight() {
        return right;
    }
    public void setRight(TreeNode<T> right) {
        this.right = right;
    }
    
    public static <T> void  preOrderTraverse(TreeNode<T> tree) {
        if(tree==null) return;
        System.out.println(tree.getVal());
        preOrderTraverse(tree.getLeft());
        preOrderTraverse(tree.getRight());
    }
   
    public static <T> void  inOrderTraverse(TreeNode<T> tree) {
        if(tree==null) return;
        preOrderTraverse(tree.getLeft());
        System.out.println(tree.getVal());
        preOrderTraverse(tree.getRight());
    }
    
    public static <T> void  postOrderTraverse(TreeNode<T> tree) {
        if(tree==null) return;
        preOrderTraverse(tree.getLeft());
        preOrderTraverse(tree.getRight());
        System.out.println(tree.getVal());
    }
    
    public static void main(String[] args) {
        TreeNode<Integer> tree = new TreeNode<>();
        tree.setVal(1);
        TreeNode<Integer> tree1 = new TreeNode<>();
        tree1.setVal(2);
        TreeNode<Integer> tree2 = new TreeNode<>();
        tree2.setVal(3);
        
        tree.setLeft(tree1);
        tree.setRight(tree2);
        preOrderTraverse(tree);
        System.out.println("-------------");
        inOrderTraverse(tree);
        System.out.println("-------------");
        postOrderTraverse(tree);
    }
      
}
